package com.tcm.store.service;


import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.exception.ServiceException;
import com.base.core.lang.StringUtils;
import com.base.core.utils.R;
import com.tcm.store.domain.dto.StoreServiceDTO;
import com.tcm.store.domain.entity.StoreService;
import com.tcm.store.domain.enums.BusinessCodeEnum;
import com.tcm.store.mapper.StoreServiceMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 3000)
public class StoreServiceServiceImpl implements StoreServiceService {

    @Autowired
    private StoreServiceMapper storeServiceMapper;


    @Override
    public StoreServiceDTO findServiceById(Long id) {
        StoreService storeService = storeServiceMapper.findServiceById(id);
        if(ObjectUtil.isEmpty(storeService)){
            return null;
        }
        StoreServiceDTO storeServiceDTO = new StoreServiceDTO();
        BeanUtils.copyProperties(storeService,storeServiceDTO);
        return storeServiceDTO;
    }

    @Override
    public StoreServiceDTO queryServiceByCategory(Long storeId, String category, Long dosageId) {
        StoreService storeService = storeServiceMapper.queryStoreService(storeId, category, dosageId);
        if(ObjectUtil.isEmpty(storeService)){
            return null;
        }
        StoreServiceDTO storeServiceDTO = new StoreServiceDTO();
        BeanUtils.copyProperties(storeService, storeServiceDTO);
        return storeServiceDTO;
    }

    /**
     * 查询药房是否支持某剂型提供的某类型服务
     * @param storeId
     * @param category
     * @param dosageId
     * @return
     */
    @Override
    public boolean checkService(Long storeId, String category, Long dosageId) {
        Integer count = storeServiceMapper.checkService(storeId, category, dosageId);
        if(count == 0){
            return false;
        }
        return true;
    }

    @Override
    public void save(StoreServiceDTO storeServiceDTO) throws ServiceException {
        if (ObjectUtil.isEmpty(storeServiceDTO.getStoreId())) {
            R.throwFail(BusinessCodeEnum.STORE_ID_NOT_EXISTS);
        }
        if (StringUtils.isEmpty(storeServiceDTO.getCategory())) {
            R.throwFail(BusinessCodeEnum.SERVICE_CATEGORY_NOT_EXISTS);
        }
        Integer count = storeServiceMapper.checkServiceCategory(storeServiceDTO.getStoreId()
                ,storeServiceDTO.getCategory(), null);
        if(count != 0){
            R.throwFail(BusinessCodeEnum.SERVICE_CATEGORY_EXISTS);
        }
        try{
            StoreService storeService = DozerConvert.copyProperties(storeServiceDTO, StoreService.class);
            storeServiceMapper.insert(storeService);
        }catch (Exception e){
            R.throwFail(e.getMessage());
        }

    }

    @Override
    public void update(StoreServiceDTO storeServiceDTO) throws ServiceException{
        if (ObjectUtil.isEmpty(storeServiceDTO.getStoreId())) {
            R.throwFail(BusinessCodeEnum.STORE_ID_NOT_EXISTS);
        }
        if (StringUtils.isEmpty(storeServiceDTO.getId())) {
            R.throwFail(BusinessCodeEnum.SERVICE_ID_NOT_EXISTS);
        }
        StoreService storeService = storeServiceMapper.findServiceById(storeServiceDTO.getId());
        if(ObjectUtil.isEmpty(storeService)){
            R.throwFail(BusinessCodeEnum.SERVICE_NOT_EXISTS);
        }
        int count = storeServiceMapper.checkServiceCategory(storeServiceDTO.getStoreId(),
                storeServiceDTO.getCategory(),
                storeServiceDTO.getId());
        if(count != 0){
            R.throwFail(BusinessCodeEnum.SERVICE_CATEGORY_EXISTS);
        }
        try{
            BeanUtils.copyProperties(storeServiceDTO, storeService);
            storeServiceMapper.update(storeService);
        }catch (Exception e){
            R.throwFail(e.getMessage());
        }
    }

    @Override
    public int deleteLogicById(Long id) {
        return storeServiceMapper.deleteLogicById(id);
    }

    @Override
    public int deleteById(Long id) {
        return storeServiceMapper.deleteById(id);
    }


}
